<template>
  <div>
    <!-- 顶部面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <!-- 搜索框 -->
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 添加商品按钮 -->
          <el-button type="primary" class="add-goods-button" @click="addGoodTOPath">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 表格视图 -->
      <el-table
        :data="goodlist"
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column width="95px" label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column width="70px" label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column width="170px" label="创建时间" prop="add_time">
          <!-- 时间格式化 -->
          <template v-slot="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column width="190px" label="操作">
          <!-- 编辑按钮区域 -->
          <template v-slot="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="delGood(scope.row.goods_id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页功能 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="100"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      //* 商品列表
      goodlist: [],
      //* 商品总数
      total: 0
    }
  },
  created () {
    this.getGoodsList()
  },
  methods: {
    getGoodsList () {
      this.$http.getGoodsList(this.queryInfo)
        .then(resp => {
          const { data: res } = resp
          // console.log(res.data)
          this.goodlist = res.data.goods
          this.total = res.data.total
          // console.log(this.goodlist)
        })
    },
    //* 页码个数
    handleSizeChange (newsize) {
      console.log(newsize)
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    //* 翻页
    handleCurrentChange (newnum) {
      this.queryInfo.pagenum = newnum
      this.getGoodsList()
    },
    //* 删除商品
    async delGood (id) {
      const confirmResult = await this.$confirm('此操作将删除此商品, 是否继续?', '删除角色', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      } else {
        const { data: res } = await this.$http.delGood(id)
        if (res.meta.status !== 200) {
          return this.$message.error('删除失败')
        } else {
          this.$message.success('删除成功')
          this.getGoodsList()
        }
      }
    },
    //* 点击添加商品
    addGoodTOPath () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card {
    margin-top: 15px;
    .add-goods-button {
      margin-left: 15px;
    }
    .el-table {margin-top: 15px;}
  }
  .el-pagination {
    margin-top: 15px;
  }
</style>
